home *** CD-ROM | disk | FTP | other *** search
/ Motor Sport Digital Archive Collection 1960s / Motor Sport Digital Archive Collection 1960s.iso / main.swf / scripts / mx / collections / ArrayList.as < prev    next >
Encoding:
Text File  |  2008-05-21  |  10.4 KB  |  339 lines

  1. package mx.collections
  2. {
  3.    import flash.events.EventDispatcher;
  4.    import flash.events.IEventDispatcher;
  5.    import flash.system.ApplicationDomain;
  6.    import flash.utils.IDataInput;
  7.    import flash.utils.IDataOutput;
  8.    import flash.utils.IExternalizable;
  9.    import flash.utils.getQualifiedClassName;
  10.    import mx.core.IPropertyChangeNotifier;
  11.    import mx.core.mx_internal;
  12.    import mx.events.CollectionEvent;
  13.    import mx.events.CollectionEventKind;
  14.    import mx.events.PropertyChangeEvent;
  15.    import mx.events.PropertyChangeEventKind;
  16.    import mx.resources.ResourceBundle;
  17.    import mx.utils.ArrayUtil;
  18.    import mx.utils.StringUtil;
  19.    import mx.utils.UIDUtil;
  20.    
  21.    use namespace mx_internal;
  22.    
  23.    public class ArrayList extends EventDispatcher implements IList, IExternalizable, IPropertyChangeNotifier
  24.    {
  25.       private static var resourceOutOfBounds:String;
  26.       
  27.       mx_internal static const VERSION:String = "2.0.1.0";
  28.       
  29.       private static var packageResources:ResourceBundle = ResourceBundle.getResourceBundle("collections",ApplicationDomain.currentDomain);
  30.       
  31.       loadResources();
  32.       
  33.       private var _uid:String;
  34.       
  35.       private var _source:Array;
  36.       
  37.       private var _dispatchEvents:int = 0;
  38.       
  39.       public function ArrayList(param1:Array = null)
  40.       {
  41.          _dispatchEvents = 0;
  42.          super();
  43.          disableEvents();
  44.          this.source = param1;
  45.          enableEvents();
  46.          _uid = UIDUtil.createUID();
  47.       }
  48.       
  49.       private static function loadResources() : void
  50.       {
  51.          resourceOutOfBounds = packageResources.getString("outOfBounds");
  52.       }
  53.       
  54.       private function internalDispatchEvent(param1:String, param2:Object = null, param3:int = -1) : void
  55.       {
  56.          var _loc4_:CollectionEvent = null;
  57.          var _loc5_:PropertyChangeEvent = null;
  58.          if(_dispatchEvents == 0)
  59.          {
  60.             if(hasEventListener(CollectionEvent.COLLECTION_CHANGE))
  61.             {
  62.                _loc4_ = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
  63.                _loc4_.kind = param1;
  64.                _loc4_.items.push(param2);
  65.                _loc4_.location = param3;
  66.                dispatchEvent(_loc4_);
  67.             }
  68.             if(hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE) && (param1 == CollectionEventKind.ADD || param1 == CollectionEventKind.REMOVE))
  69.             {
  70.                _loc5_ = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
  71.                _loc5_.property = param3;
  72.                if(param1 == CollectionEventKind.ADD)
  73.                {
  74.                   _loc5_.newValue = param2;
  75.                }
  76.                else
  77.                {
  78.                   _loc5_.oldValue = param2;
  79.                }
  80.                dispatchEvent(_loc5_);
  81.             }
  82.          }
  83.       }
  84.       
  85.       public function removeAll() : void
  86.       {
  87.          var _loc1_:int = 0;
  88.          var _loc2_:int = 0;
  89.          if(length > 0)
  90.          {
  91.             _loc1_ = length;
  92.             _loc2_ = 0;
  93.             while(_loc2_ < _loc1_)
  94.             {
  95.                stopTrackUpdates(source[_loc2_]);
  96.                _loc2_++;
  97.             }
  98.             source.splice(0,length);
  99.             internalDispatchEvent(CollectionEventKind.RESET);
  100.          }
  101.       }
  102.       
  103.       public function readExternal(param1:IDataInput) : void
  104.       {
  105.          source = param1.readObject();
  106.       }
  107.       
  108.       protected function startTrackUpdates(param1:Object) : void
  109.       {
  110.          if(Boolean(param1) && param1 is IEventDispatcher)
  111.          {
  112.             IEventDispatcher(param1).addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,itemUpdateHandler,false,0,true);
  113.          }
  114.       }
  115.       
  116.       public function get source() : Array
  117.       {
  118.          return _source;
  119.       }
  120.       
  121.       public function getItemAt(param1:int, param2:int = 0) : Object
  122.       {
  123.          if(param1 < 0 || param1 >= length)
  124.          {
  125.             throw new RangeError(StringUtil.substitute(resourceOutOfBounds,param1));
  126.          }
  127.          return source[param1];
  128.       }
  129.       
  130.       public function toArray() : Array
  131.       {
  132.          return source.concat();
  133.       }
  134.       
  135.       public function set uid(param1:String) : void
  136.       {
  137.          _uid = param1;
  138.       }
  139.       
  140.       private function disableEvents() : void
  141.       {
  142.          --_dispatchEvents;
  143.       }
  144.       
  145.       public function itemUpdated(param1:Object, param2:Object = null, param3:Object = null, param4:Object = null) : void
  146.       {
  147.          var _loc5_:PropertyChangeEvent = null;
  148.          _loc5_ = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
  149.          _loc5_.kind = PropertyChangeEventKind.UPDATE;
  150.          _loc5_.source = param1;
  151.          _loc5_.property = param2;
  152.          _loc5_.oldValue = param3;
  153.          _loc5_.newValue = param4;
  154.          itemUpdateHandler(_loc5_);
  155.       }
  156.       
  157.       public function addItemAt(param1:Object, param2:int) : void
  158.       {
  159.          if(param2 < 0 || param2 > length)
  160.          {
  161.             throw new RangeError(StringUtil.substitute(resourceOutOfBounds,param2));
  162.          }
  163.          source.splice(param2,0,param1);
  164.          startTrackUpdates(param1);
  165.          internalDispatchEvent(CollectionEventKind.ADD,param1,param2);
  166.       }
  167.       
  168.       public function setItemAt(param1:Object, param2:int) : Object
  169.       {
  170.          var _loc3_:Object = null;
  171.          var _loc4_:Boolean = false;
  172.          var _loc5_:Boolean = false;
  173.          var _loc6_:PropertyChangeEvent = null;
  174.          var _loc7_:CollectionEvent = null;
  175.          if(param2 < 0 || param2 >= length)
  176.          {
  177.             throw new RangeError(StringUtil.substitute(resourceOutOfBounds,param2));
  178.          }
  179.          _loc3_ = source[param2];
  180.          source[param2] = param1;
  181.          stopTrackUpdates(_loc3_);
  182.          startTrackUpdates(param1);
  183.          if(_dispatchEvents == 0)
  184.          {
  185.             _loc4_ = hasEventListener(CollectionEvent.COLLECTION_CHANGE);
  186.             _loc5_ = hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE);
  187.             if(_loc4_ || _loc5_)
  188.             {
  189.                _loc6_ = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
  190.                _loc6_.kind = PropertyChangeEventKind.UPDATE;
  191.                _loc6_.oldValue = _loc3_;
  192.                _loc6_.newValue = param1;
  193.                _loc6_.property = param2;
  194.             }
  195.             if(_loc4_)
  196.             {
  197.                _loc7_ = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
  198.                _loc7_.kind = CollectionEventKind.REPLACE;
  199.                _loc7_.location = param2;
  200.                _loc7_.items.push(_loc6_);
  201.                dispatchEvent(_loc7_);
  202.             }
  203.             if(_loc5_)
  204.             {
  205.                dispatchEvent(_loc6_);
  206.             }
  207.          }
  208.          return _loc3_;
  209.       }
  210.       
  211.       protected function stopTrackUpdates(param1:Object) : void
  212.       {
  213.          if(Boolean(param1) && param1 is IEventDispatcher)
  214.          {
  215.             IEventDispatcher(param1).removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE,itemUpdateHandler);
  216.          }
  217.       }
  218.       
  219.       public function set source(param1:Array) : void
  220.       {
  221.          var _loc2_:int = 0;
  222.          var _loc3_:int = 0;
  223.          var _loc4_:CollectionEvent = null;
  224.          if(Boolean(_source) && Boolean(_source.length))
  225.          {
  226.             _loc3_ = int(_source.length);
  227.             _loc2_ = 0;
  228.             while(_loc2_ < _loc3_)
  229.             {
  230.                stopTrackUpdates(_source[_loc2_]);
  231.                _loc2_++;
  232.             }
  233.          }
  234.          _source = !!param1 ? param1 : [];
  235.          _loc3_ = int(_source.length);
  236.          _loc2_ = 0;
  237.          while(_loc2_ < _loc3_)
  238.          {
  239.             startTrackUpdates(_source[_loc2_]);
  240.             _loc2_++;
  241.          }
  242.          if(_dispatchEvents == 0)
  243.          {
  244.             _loc4_ = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
  245.             _loc4_.kind = CollectionEventKind.RESET;
  246.             dispatchEvent(_loc4_);
  247.          }
  248.       }
  249.       
  250.       public function removeItem(param1:Object) : Boolean
  251.       {
  252.          var _loc2_:int = 0;
  253.          var _loc3_:* = false;
  254.          _loc2_ = getItemIndex(param1);
  255.          _loc3_ = _loc2_ >= 0;
  256.          if(_loc3_)
  257.          {
  258.             removeItemAt(_loc2_);
  259.          }
  260.          return _loc3_;
  261.       }
  262.       
  263.       public function getItemIndex(param1:Object) : int
  264.       {
  265.          return ArrayUtil.getItemIndex(param1,source);
  266.       }
  267.       
  268.       public function writeExternal(param1:IDataOutput) : void
  269.       {
  270.          param1.writeObject(_source);
  271.       }
  272.       
  273.       public function get uid() : String
  274.       {
  275.          return _uid;
  276.       }
  277.       
  278.       public function addItem(param1:Object) : void
  279.       {
  280.          addItemAt(param1,length);
  281.       }
  282.       
  283.       public function removeItemAt(param1:int) : Object
  284.       {
  285.          var _loc2_:Object = null;
  286.          if(param1 < 0 || param1 >= length)
  287.          {
  288.             throw new RangeError(StringUtil.substitute(resourceOutOfBounds,param1));
  289.          }
  290.          _loc2_ = source.splice(param1,1)[0];
  291.          stopTrackUpdates(_loc2_);
  292.          internalDispatchEvent(CollectionEventKind.REMOVE,_loc2_,param1);
  293.          return _loc2_;
  294.       }
  295.       
  296.       public function get length() : int
  297.       {
  298.          if(source)
  299.          {
  300.             return source.length;
  301.          }
  302.          return 0;
  303.       }
  304.       
  305.       protected function itemUpdateHandler(param1:PropertyChangeEvent) : void
  306.       {
  307.          var _loc2_:PropertyChangeEvent = null;
  308.          var _loc3_:uint = 0;
  309.          internalDispatchEvent(CollectionEventKind.UPDATE,param1);
  310.          if(_dispatchEvents == 0 && hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE))
  311.          {
  312.             _loc2_ = PropertyChangeEvent(param1.clone());
  313.             _loc3_ = uint(getItemIndex(param1.target));
  314.             _loc2_.property = _loc3_.toString() + "." + param1.property;
  315.             dispatchEvent(_loc2_);
  316.          }
  317.       }
  318.       
  319.       override public function toString() : String
  320.       {
  321.          if(source)
  322.          {
  323.             return source.toString();
  324.          }
  325.          return getQualifiedClassName(this);
  326.       }
  327.       
  328.       private function enableEvents() : void
  329.       {
  330.          ++_dispatchEvents;
  331.          if(_dispatchEvents > 0)
  332.          {
  333.             _dispatchEvents = 0;
  334.          }
  335.       }
  336.    }
  337. }
  338.  
  339.